package cn.wubo.xianxiacomment

import com.intellij.openapi.options.Configurable
import com.intellij.openapi.options.ConfigurationException
import com.intellij.ui.components.JBTextField
import com.intellij.util.ui.FormBuilder
import javax.swing.JComponent
import javax.swing.JPanel

class OllamaConfigurable : Configurable {
    private var mainPanel: JPanel? = null
    private val ollamaUrlField = JBTextField()
    private val modelNameField = JBTextField()

    init {
        mainPanel = FormBuilder.createFormBuilder()
            .addLabeledComponent("Ollama服务地址:", ollamaUrlField, 1, false)
            .addLabeledComponent("对话模型名称:", modelNameField, 1, false)
            .addComponentFillVertically(JPanel(), 0)
            .panel
    }

    override fun getDisplayName(): String {
        return "修仙注释"
    }

    override fun getPreferredFocusedComponent(): JComponent {
        return ollamaUrlField
    }

    override fun createComponent(): JComponent {
        return mainPanel!!
    }

    override fun isModified(): Boolean {
        val settings = OllamaConfigService.instance
        return ollamaUrlField.text != settings.ollamaUrl || modelNameField.text != settings.modelName
    }

    @Throws(ConfigurationException::class)
    override fun apply() {
        val settings = OllamaConfigService.instance
        settings.ollamaUrl = ollamaUrlField.text
        settings.modelName = modelNameField.text
    }

    override fun reset() {
        val settings = OllamaConfigService.instance
        ollamaUrlField.text = settings.ollamaUrl
        modelNameField.text = settings.modelName
    }
}